import Vue from "vue";
import VueRouter from "vue-router";

const Login = () => import("views/Login/login");
const Home = () => import("views/Home/home");
const Welcome = () => import("views/Home/Comps/Welcome");
const Users = () => import("views/Home/Comps/Users");
const Power = () => import("views/Home/Comps/Power");
const Roles = () => import("views/Home/Comps/Roles");
Vue.use(VueRouter);

const routes = [
  { path: "", redirect: "/login" },
  { path: "/login", component: Login },
  {
    path: "/home",
    component: Home,
    redirect: "/welcome",
    children: [
      { path: "/welcome", component: Welcome },
      { path: "/users", component: Users },
      { path: "/rights", component: Power},
      { path: "/roles", component: Roles},
    ],
  },
];

const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes,
});
// 路由导航守卫
router.beforeEach((to, from, next) => {
  if (to.path == "/login") return next();
  let token = window.sessionStorage.getItem("token");
  if (token) {
    next();
  } else {
    next("/login");
  }
});
export default router;
